/*
Copyright 2008, 2009 Joonas Lehtolahti

This file is part of Openfire-C.

Openfire-C is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Openfire-C is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Openfire-C.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "XfirePackets.h"

/*
 * XfirePHc_ClientVersion
 *   Creates network packet for Xfire version number reporting
 * 
 * Parameters:
 *   version   unsigned integer presenting the client version number
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_ClientVersion(unsigned int version) {
	/* Needed variables */
	struct XfirePacket *p;
	
	/* Packet '03 00', has 1 attribute and regular attribute names */
	p = XfirePHo_Init(XFPIDC_CLIENTVERSION, 1, 0);
	
	/* Add integer attribute "version" */
	XfirePHo_AddAttribute(p, (unsigned const char *)"version", XFP_TYPE_INT, &version);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_UserLogin
 *   Creates network packet for logging in
 * 
 * Parameters:
 *   username   The Xfire username to log in
 *   password   The password for user to log in
 *   salt       The login salt received from the server
 *   flags      login flags, set it to 0
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_UserLogin(non0string *username, non0string *password,
		non0string *salt, unsigned int flags) {
	/* Needed variables */
	struct XfirePacket *p;
	non0string *sid, *hash;
	
	hash = XfirePHo_HashPasswordWithSalt(username, password, salt);
	
	if (!hash) return NULL;
	
	/* Packet '01 00', has 4 attributes and regular attribute names */
	p = XfirePHo_Init(XFPIDC_LOGIN, 4, 0);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"name", XFP_TYPE_STRING, username);
	XfirePHo_AddAttribute(p, (unsigned const char *)"password", XFP_TYPE_STRING, hash);
	XfirePHo_AddAttribute(p, (unsigned const char *)"flags", XFP_TYPE_INT, &flags);
	
	sid = n0salloc(XFP_SIZE_ATTRSIDVALUE);
	memset(sid->s, 0, sid->l);
	XfirePHo_AddAttribute(p, (unsigned const char *)"sid", XFP_TYPE_SID, sid);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_UserLoginSaved
 *   Creates network packet for logging in using saved hash
 * 
 * Parameters:
 *   username   The Xfire username to log in
 *   pwhash     The encrypted password for the user
 *   salt       The login salt received from the server
 *   flags      login flags, set it to 0
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_UserLoginSaved(non0string *username, non0string *pwhash,
		non0string *salt, unsigned int flags) {
	/* Needed variables */
	struct XfirePacket *p;
	non0string *sid, *hash;
	
	hash = XfirePHo_HashWithSalt(pwhash, salt);
	
	if (!hash) return NULL;
	
	/* Packet '01 00', has 4 attributes and regular attribute names */
	p = XfirePHo_Init(XFPIDC_LOGIN, 4, 0);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"name", XFP_TYPE_STRING, username);
	XfirePHo_AddAttribute(p, (unsigned const char *)"password", XFP_TYPE_STRING, hash);
	XfirePHo_AddAttribute(p, (unsigned const char *)"flags", XFP_TYPE_INT, &flags);
	
	sid = n0salloc(XFP_SIZE_ATTRSIDVALUE);
	memset(sid->s, 0, sid->l);
	XfirePHo_AddAttribute(p, (unsigned const char *)"sid", XFP_TYPE_SID, sid);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_ClientInfo
 *   Creates network packet for telling the server statistical information
 *   about the connecting client
 * 
 * Parameters:
 *   lang       Language identifier code
 *   skin       Skin long name
 *   theme      Skin theme name
 *   partner    Skin partner name
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_ClientInfo(non0string *lang, non0string *skin, 
		non0string *theme, non0string *partner) {
	/* Needed variables */
	struct XfirePacket *p;
	
	/* Packet '10 00', has 4 attributes and regular attribute names */
	p = XfirePHo_Init(XFPIDC_CLIENTINFO, 4, 0);
	
	/* Default values for parameters */
	if (!lang) lang = char2non((unsigned const char *)"us");
	if (!skin) skin = char2non((unsigned const char *)"Openfire-C");
	if (!theme) theme = char2non((unsigned const char *)"unspecified");
	if (!partner) partner = char2non((unsigned const char *)"");
	
	/* Set attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"lang", XFP_TYPE_STRING, lang);
	XfirePHo_AddAttribute(p, (unsigned const char *)"skin", XFP_TYPE_STRING, skin);
	XfirePHo_AddAttribute(p, (unsigned const char *)"theme", XFP_TYPE_STRING, theme);
	XfirePHo_AddAttribute(p, (unsigned const char *)"partner", XFP_TYPE_STRING, partner);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_PeerInfo
 *   Creates network packet for telling the server information about
 *   the network the user is in, for p2p purposes.
 * 
 * Parameters:
 *   conn       Connection type
 *   nat        NAT Type
 *   sec        
 *   clientip   IP address
 *   naterr     NAT Error code
 *   upnpinfo   UPnP information
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_PeerInfo(unsigned int conn, unsigned int nat,
		unsigned int sec, unsigned int clientip, unsigned int naterr,
		non0string *upnpinfo) {
	/* Needed variables */
	struct XfirePacket *p;
	
	/* Packet '11 00', has 6 attributes and regular attribute names */
	p = XfirePHo_Init(XFPIDC_PEERINFO, 6, 0);
	
	/* Default values for parameters */
	if (!upnpinfo) upnpinfo = char2non((unsigned const char *)"");
	
	/* Set attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"conn", XFP_TYPE_INT, &conn);
	XfirePHo_AddAttribute(p, (unsigned const char *)"nat", XFP_TYPE_INT, &nat);
	XfirePHo_AddAttribute(p, (unsigned const char *)"sec", XFP_TYPE_INT, &sec);
	XfirePHo_AddAttribute(p, (unsigned const char *)"clientip", XFP_TYPE_INT, &clientip);
	XfirePHo_AddAttribute(p, (unsigned const char *)"naterr", XFP_TYPE_INT, &naterr);
	XfirePHo_AddAttribute(p, (unsigned const char *)"upnpinfo", XFP_TYPE_STRING, upnpinfo);
	
	/* Return completed packet */
	return p;
}
